<?php

namespace App\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * Created by PhpStorm.
 * User: wangjiangtao
 * Date: 2021/11/12
 * Time: 10:33
 */
class CheckRecharage extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'check_recharge';
    protected $primaryKey = 'id';

    public function getList(Request $request){
        $uuid = $request->input('uuid')??'';
        $order_id = $request->input('order_id')??'';
        $status = $request->input('status')??'';
        $rule = $request->input('rule')??'';
        $startDate =$request->input('start_date');
        $endDate =$request->input('end_date');
        $page = $request->input('page')??1;
        $limit = $request->input('limit')??10;

        $checkObj = DB::connection('api')->table($this->table);
        if ($uuid){
            $user = DB::connection('api')->table('user')->where('uuid',$uuid)->first(['user_id']);
            if ($user) $checkObj->where('user_id',$user->user_id);
        }
        if ($rule) $checkObj->where('rule',$rule);
        if ($order_id) $checkObj->where('order_id',$order_id);
        if ($status != '') $checkObj->where('status',$status);
        if ($startDate)$checkObj->where('created_at','>=',strtotime($startDate));
        if ($endDate)$checkObj->where('created_at','<=',strtotime($endDate));
        $data = $checkObj->orderBy('created_at','desc')
            ->paginate($limit);
        $user_id = array_column($data->items(),'user_id');
        $opt_id = array_column($data->items(),'opt_id');
        $userData = DB::connection('api')->table('user')
            ->whereIn('user_id',$user_id)
            ->get(['nickname','uuid','user_id']);

        $userData = $this->objToArray($userData);
        $userData = array_column($userData,null,'user_id');
        $userVerifyData = DB::connection('api')->table('user_verify')
            ->whereIn('user_id',$user_id)
            ->where('result_code',1000)
            ->get(['idcard_name','idcard_number','user_id']);

        $userVerifyData = $this->objToArray($userVerifyData);
        $userVerifyData = array_column($userVerifyData,null,'user_id');

        $optData = DB::table('users')->whereIn('id',$opt_id)->get(['id','name']);
        $optData = $this->objToArray($optData);
        $optData = array_column($optData,null,'id');

        foreach ($data->items() as $item){
            $item->recharge_img = build_image($item->recharge_img);
            $item->user_name = isset($userData[$item->user_id])?$userData[$item->user_id]['nickname']:'';
            $item->uuid = isset($userData[$item->user_id])?$userData[$item->user_id]['uuid']:'';
            $item->idcard_name = isset($userVerifyData[$item->user_id])?$userVerifyData[$item->user_id]['idcard_name']:'';
            $item->idcard_number = isset($userVerifyData[$item->user_id])?$userVerifyData[$item->user_id]['idcard_number']:0;
            $item->opt_name =  isset($optData[$item->opt_id])?$optData[$item->opt_id]['name']:'';
        }
        return $data;
    }

    public function getRule(){
        return curl_request(getKkaudioApiUrl() . '/inner/check/recharage/getrule',[], 'urlencoded', 'GET');
    }


    public function updateRecharage($data){
        return curl_request(getKkaudioApiUrl() . '/inner/check/recharage/update', $data, 'urlencoded', 'POST');
    }
    public function getdataByid($id){
        $res =  DB::connection('api')->table($this->table)->where('id',$id)->first();
        return $this->objToArray($res);
    }
    public function getdataByOrderid($order_id){
        $res =  DB::connection('api')->table($this->table)->where('order_id',$order_id)->where('status',1)->first();
        return $this->objToArray($res);
    }
}
